//
//  NewsPresenter.m
//  News
//
//  Created by Han on 15-4-17.
//  Copyright (c) 2015年 IPingLun. All rights reserved.
//

#import "NewsDetailPresenter.h"

@implementation NewsDetailPresenter

- (void) getNewsDetail:(int)newsId :(void (^)(NewsDetail *data))success
          failureBlock:(void (^)(NSError *error))failure
{
    
    [[HttpHelper sharedClient] GET:[NSString stringWithFormat:@"/v2/posts/%d", newsId] parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        
        NSArray *responseData = (NSArray *)JSON;
        NewsDetail * newsDetail = NewsDetail.new;
        newsDetail.news_id = [[responseData valueForKeyPath:@"id"] intValue];
        newsDetail.title = [responseData valueForKeyPath:@"title"];
        
        NSString *html = [NSString stringWithFormat:@"<html><link href='style.css' rel='stylesheet' type='text/css'/><head><title></title></head><body style='color:#333333;line-height:150%%;font-size:12px;'>%@</body></html>", [responseData valueForKeyPath:@"content"]];
        
        html = [html stringByReplacingOccurrencesOfString:@"style=\".*?\"" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, html.length)];
        
        newsDetail.content = html;
        
        success(newsDetail);
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        failure(error);
    }];
    
}


@end
